package com.aiti.lulian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.lulian.bo.LoginUserInfo;
import com.aiti.lulian.dto.industry.SupportContentParamDto;
import com.aiti.lulian.dto.industry.SupportContentQueryDto;
import com.aiti.lulian.entity.industry.CardGovIndustrySupportContent;
import com.aiti.lulian.mapper.card.industry.CardGovIndustrySupportContentMapper;
import com.aiti.lulian.service.CardGovIndustrySupportContentService;
import com.aiti.lulian.utils.BusinessUtil;
import com.aiti.lulian.vo.industry.SupportContentVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 产业支持内容表服务实现类
 * @auth fcb
 * @date 2025-03-05 10:40
 **/
@Service
@Slf4j
public class CardGovIndustrySupportContentServiceImpl extends BaseSerivceImpl<CardGovIndustrySupportContentMapper, CardGovIndustrySupportContent> implements CardGovIndustrySupportContentService {

    /**
     * 新增产业支持内容
     * @param paramDto
     * @param loginUserInfo
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void addIndustrySupportContent(SupportContentParamDto paramDto, LoginUserInfo loginUserInfo) {
        // 1. 当前产业下，产业支持功能中，要添加的内容是否已存在
        Integer count = this.baseMapper.getCount(paramDto.getName(), paramDto.getIndustryId(), paramDto.getSupportId());
        if(count > 0) {
            throw new BusinessException("数据已存在，请勿重复添加");
        }
        // 2. 新增
        CardGovIndustrySupportContent po = new CardGovIndustrySupportContent();
        BeanUtil.copyProperties(paramDto, po);
        po.setBaseId(UUID.randomUUID().toString());
        po.setBaseCreateBy(loginUserInfo.getUserId());
        this.save(po);
    }

    /**
     * 修改产业支持内容
     * @param paramDto
     * @param loginUserInfo
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void updateIndustrySupportContent(SupportContentParamDto paramDto, LoginUserInfo loginUserInfo) {
        if(StrUtil.isEmpty(paramDto.getBaseId())) {
            throw new BusinessException("参数错误");
        }
        CardGovIndustrySupportContent po = new CardGovIndustrySupportContent();
        BeanUtil.copyProperties(paramDto, po);
        po.setBaseUpdateBy(loginUserInfo.getUserId());

        this.baseMapper.updateIndustrySupportContent(po);
    }

    /**
     * 删除产业支持内容
     * @param baseIds
     */
    public void deleteIndustrySupportContent(List<String> baseIds) {
        if(CollectionUtil.isEmpty(baseIds)) {
            return;
        }
        this.baseMapper.deleteIndustrySupportContent(baseIds);
    }

    /**
     * 查询产业支持内容列表
     * @param queryDto
     * @return
     */
    @Override
    public PageResult<SupportContentVo> queryIndustrySupportContentList(SupportContentQueryDto queryDto) {
        BusinessUtil.checkPageParam(queryDto);
        IPage<SupportContentVo> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        List<SupportContentVo> list = this.baseMapper.queryIndustrySupportContentList(page, queryDto);
        PageResult<SupportContentVo> pageResult = new PageResult<>(list, new PageInfo(queryDto.getPageNum(), queryDto.getPageSize()), page.getTotal());
        return pageResult;
    }

    @Override
    public SupportContentVo getIndustrySupportContentById(String baseId) {
        if(StrUtil.isEmpty(baseId)) {
            throw new BusinessException("参数错误");
        }
        CardGovIndustrySupportContent po = this.getById(baseId);
        if(po == null || po.getIsDelete() == 1) {
            throw new BusinessException("数据不存在");
        }
        SupportContentVo vo = new SupportContentVo();
        BeanUtil.copyProperties(po, vo);
        return vo;
    }

}
